from datetime import datetime, timedelta

company_name = "念旧"

def get_time_stamp(date):
    _date = datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%fZ")
    local_time = _date + timedelta(hours=8)
    local_date_srt = local_time.strftime("%Y-%m-%d %H:%M:%S")
    return local_date_srt


def firing_template(alert_info: list):
    content_result = ""
    content_title = f"> <font color=#FF0000> {company_name} 监控告警侦测到{len(alert_info)}个故障</font> \n\n"
    for info in alert_info:
        content = f"> **告警类型**: {info['labels']['alertname']}&" \
                + f"> **告警级别**: {info['labels']['severity']}&" \
                + f"> **告警主机**: {info['labels']['job']}&" \
                + f"> **告警主题**: {info['annotations']['summary']}&" \
                + f"> **告警信息**: {info['annotations']['description']}&" \
                + f"> **告警时间**: {get_time_stamp(info['startsAt'])}&" \
                + "---"
        content_result += content
    result = ''.join(str(content_title + content_result).replace('&', '\n\n'))
    return result


def resolved_template(alert_info: list):
    content_result = ""
    content_title = f"> <font color=#008000> {company_name} 监控告警恢复{len(alert_info)}个故障</font> \n\n"
    for info in alert_info:
        content = f"> **告警类型**: {info['labels']['alertname']}&" \
                  + f"> **告警级别**: {info['labels']['severity']}&" \
                  + f"> **告警主机**: {info['labels']['job']}&" \
                  + f"> **告警主题**: {info['annotations']['summary']}&" \
                  + f"> **告警信息**: {info['annotations']['description']}&" \
                  + f"> **告警时间**: {get_time_stamp(info['startsAt'])}&" \
                  + f"> **恢复时间**: {get_time_stamp(info['endsAt'])}&" \
                  + "---"
        content_result += content
    result = ''.join(str(content_title + content_result).replace('&', '\n\n'))
    return result
